﻿Imports TransportadoraToledoDAL
Public Class UsuariosBLL
    Dim objDAL As UsuariosDAL

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property
    Private pessoa As Integer
    Public Property pPessoa() As Integer
        Get
            Return pessoa
        End Get
        Set(ByVal value As Integer)
            pessoa = value
        End Set
    End Property
    Private login As String
    Public Property pLogin() As String
        Get
            Return login
        End Get
        Set(ByVal value As String)
            login = value
        End Set
    End Property
    Private senha As String
    Public Property pSenha() As String
        Get
            Return senha
        End Get
        Set(ByVal value As String)
            senha = value
        End Set
    End Property
    Private ativo As Boolean
    Public Property pAtivo() As Boolean
        Get
            Return ativo
        End Get
        Set(ByVal value As Boolean)
            ativo = value
        End Set
    End Property
    Private data_cadastro As Date
    Public Property pData_cadastro() As Date
        Get
            Return data_cadastro
        End Get
        Set(ByVal value As Date)
            data_cadastro = value
        End Set
    End Property

    Sub limpar()
        codigo = 0
        pessoa = 0
        login = ""
        senha = ""
        ativo = False
        data_cadastro = Nothing
    End Sub

    Sub inserir()
        Dim objDAL As New UsuariosDAL
        Try
            If (pessoa = 0) Then
                Throw New Exception("Código da pessoa não identificado")
            End If
            If (login.Trim() = "") Then
                Throw New Exception("Login não definido.")
            End If
            If (senha.Trim() = "") Then
                Throw New Exception("Senha não definida.")
            End If
            pCodigo = objDAL.inserir(pessoa, login, senha, ativo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Dim objDAL As New UsuariosDAL
        Try
            If (codigo <= 0) Then
                Throw New Exception("Usuário não definido.")
            End If
            If (pessoa = 0) Then
                Throw New Exception("Código da pessoa não identificado")
            End If
            If (login.Trim() = "") Then
                Throw New Exception("Login não definido.")
            End If
            If (senha.Trim() = "") Then
                Throw New Exception("Senha não definida.")
            End If
            objDAL.alterar(codigo, pessoa, login, senha, ativo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Dim objDAL As New UsuariosDAL
        Try
            If (codigo = 0) Then
                Throw New Exception("Usuário não definido.")
            End If
            objDAL.excluir(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As New DataTable
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New UsuariosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("usu_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("usu_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New UsuariosDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("usu_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("usu_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnterior(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As New DataTable
        Dim CodigoP As Integer = 0
        Try

            objDAL = New UsuariosDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("usu_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    Function localizar(campo As String, valor As String)
        Dim tab As New DataTable
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.localizar(campo, valor)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Sub localizar(ByVal codigo As Integer)
        Dim tab As New DataTable
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                pCodigo = tab.Rows(0)("usu_codigo").ToString
                pPessoa = tab.Rows(0)("pes_codigo").ToString
                pLogin = tab.Rows(0)("usu_login").ToString
                pSenha = tab.Rows(0)("usu_senha").ToString
                pAtivo = tab.Rows(0)("usu_ativo").ToString
                pData_cadastro = tab.Rows(0)("usu_data_cadastro").ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Function localizarPessoa(nome As String) As DataTable
        Dim tab As New DataTable
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.localizarPessoa(nome)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function

    Function localizarPessoa(codigo As Integer) As String
        Dim tab As New DataTable
        Dim nome As String
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.localizarPessoa(codigo)
            If tab.Rows.Count = 1 Then
                nome = tab.Rows(0)("pes_nome").ToString
            Else : nome = ""
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return nome
    End Function

    Function loginIgual(login As String) As Boolean 'retorna true se encontrar login igual
        Dim tab As New DataTable
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.localizar("u.usu_login", login)
            If tab.Rows.Count > 0 Then
                For Each linha As DataRow In tab.Rows
                    If linha("usu_login").Equals(login) Then
                        Return True
                    End If
                Next
            Else : Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return False
    End Function

    Sub inserirPermissao(usu As Integer, permissao As Integer)
        Dim objDAL As New UsuariosDAL
        Try
            If usu = 0 Then
                Throw New Exception("Usuário não identificado.")
            End If
            If permissao = 0 Then
                Throw New Exception("Permissão não descrita.")
            End If
            objDAL.inserirPermissao(usu, permissao)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluirPermissao(usu As Integer)
        Dim objDAL As New UsuariosDAL
        Try
            If usu = 0 Then
                Throw New Exception("Usuário não identificado.")
            End If
            objDAL.excluirPermissao(usu)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarPermissao(usu As Integer) As DataTable
        Dim tab As New DataTable
        Try
            If usu = 0 Then
                Throw New Exception("Usuário não identificado.")
            End If
            objDAL = New UsuariosDAL
            tab = objDAL.localizarPermissao(usu)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function

    Function verificarLogin() As Boolean
        Dim tab As New DataTable
        Try
            objDAL = New UsuariosDAL
            tab = objDAL.verificarLogin(login, senha)
            If (tab.Rows.Count > 0) Then
                pCodigo = tab.Rows(0)("usu_codigo").ToString
                pPessoa = tab.Rows(0)("pes_codigo").ToString
                pLogin = tab.Rows(0)("usu_login").ToString
                pSenha = tab.Rows(0)("usu_senha").ToString
                pAtivo = tab.Rows(0)("usu_ativo").ToString
                pData_cadastro = tab.Rows(0)("usu_data_cadastro").ToString
                Return True
            Else
                limpar()
                Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Sub apagar()
        Dim objDAL As New UsuariosDAL
        Try
            objDAL.apagar()
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

End Class
